﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;


public delegate void lbtnTotalComments_Click(object sender, EventArgs e);
public delegate void lbtnReadMore_Click(object sender, EventArgs e);
public delegate void lbtnReadAll_Click(object sender, EventArgs e);
public partial class MyControls_MyUserControl : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    public Panel PnlComments
    {
        get { return pnlComments; }
        set { pnlComments = value; }
    }
    public MultiView MutiViewControl
    {
        get { return mvwUserControl; }
        set { mvwUserControl = value; }
    }
    public View ViewNews
    {
        get { return vwNews; }
        set { vwNews = value; }
    }
    public View ViewComments
    {
        get { return vwComments; }
        set { vwComments = value; }
    }
    public Label LblTitleChanel
    {
        get { return lblTitleChanel; }
        set { lblTitleChanel = value; }
    }
    public Label LblTitleNew
    {
        get { return lblTitleNews; }
        set { lblTitleNews = value; }
    }
    public Label LblNewsID
    {
        get { return lblNewsID; }
        set { lblNewsID = value; }
    }
    public Label LblNewsContent
    {
        get { return lblContent; }
        set { lblContent = value; }
    }
    public Label LblTimeNewsPost
    {
        get { return lblTimePosted; }
        set { lblTimePosted = value; }
    }
    public Label LblNewsAuthor
    {
        get { return lblAuthor; }
        set { lblAuthor = value; }
    }
    public LinkButton LbtnReadMore
    {
        get { return lbtnReadMore; }
        set { lbtnReadMore = value; }
    }
    public LinkButton LbtnTotalComments
    {
        get { return lbtnTotalComments; }
        set { lbtnTotalComments = value; }
    }
    public LinkButton LbtnTitleNews2
    {
        get { return lbtnTitleNews2; }
        set { lbtnTitleNews2 = value; }
    }
    public Label LblNews2ID
    {
        get { return lblNews2ID; }
        set { lblNews2ID = value; }
    }
    public LinkButton LbtnTitleNews3
    {
        get { return lbtnTitleNews3; }
        set { lbtnTitleNews3 = value; }
    }
    public Label LblNews3ID
    {
        get { return lblNews3ID; }
        set { lblNews3ID = value; }
    }
    
    public LinkButton LbtnTitleNews4
    {
        get { return lbtnTitleNews4; }
        set { lbtnTitleNews4 = value; }
    }

    public Label LblNews4ID
    {
        get { return lblNews4ID; }
        set { lblNews4ID = value; }
    }
    public LinkButton LbtnReadAll
    {
        get { return lbtnReadAll; }
        set { lbtnReadAll = value; }
    }
    public Label LblTitleNewDetail
    {
        get { return lblTitleNewsDetails; }
        set { lblTitleNewsDetails = value; }
    }
    public Label LblTimeNewsPostDetail
    {
        get { return lblTimePostDetails; }
        set { lblTimePostDetails = value; }
    }
    public Label LblNewsAuthorDetail
    {
        get { return lblAuthorDetails; }
        set { lblAuthorDetails = value; }
    }


    protected void lbtnTotalComments_Click(object sender, EventArgs e)
    {
        //LbtnTotalCommentsMethod(e);
        mvwUserControl.SetActiveView(vwComments);
        string id = lblNewsID.Text;
        ShowNewContentDetails(id);
    }
    protected void lbtnReadMore_Click(object sender, EventArgs e)
    {
        //LbtnReadMoreMethod(e);
        mvwUserControl.SetActiveView(vwComments);
        string id = lblNewsID.Text;
        ShowNewContentDetails(id);
    }

    public event lbtnReadMore_Click lbtnReadMoreClick;
    public event lbtnTotalComments_Click lbtnTotalCommentsClick;
    protected virtual void LbtnReadMoreMethod(EventArgs e)
    {
        lbtnReadMoreClick(this, e);
    }
    protected virtual void LbtnTotalCommentsMethod(EventArgs e)
    {
        lbtnTotalCommentsClick(this, e);
    }

    void ShowNewContentDetails(string newID)
    {
        int newsID = Convert.ToInt32(newID);
        New_Report news = DatabaseInteraction.GetNewsByID(newsID);
        lblContentNewsDetails.Text = news.NewsContent;
        lblAuthorDetails.Text = news.Author;
        lblTimePostDetails.Text = news.TimePost.ToString();
        LblTitleNewDetail.Text = news.TitleNews;
        lblViews.Text = news.CountRead.ToString();
        ShowComments(newsID);
        DatabaseInteraction.UpdateCountViews(newsID);
    }

    void ShowComments(int newID)
    {
        pnlComments.Controls.Clear();
        List<Comment> list = DatabaseInteraction.GetComment(newID);        
        if (list.Count > 0)
        {
            foreach (Comment c in list)
            {
                MyControls_UserComments comment = LoadControl("~/MyControls/UserComments.ascx") as MyControls_UserComments;
                comment.Comment.Text = c.CommentContent;
                comment.Author.Text = c.Author;
                comment.Time.Text = c.TimePost.ToString();                                
                pnlComments.Controls.Add(comment);
            }
        }
        
    }
    protected void lbtnEdit_Click(object sender, EventArgs e)
    {
        EditNewsContent();
    }
    void EditNewsContent()
    {
        if (lbtnEdit.Text == "Edit")
        {
            lbtnEdit.Text = "Save";
            lblContentNewsDetails.Visible = false;
            txtContentNewsDetails.Text = lblContentNewsDetails.Text;
            txtContentNewsDetails.Visible = true;
        }
        else if (lbtnEdit.Text == "Save")
        {
            lbtnEdit.Text = "Edit";            
            string text = lblNewsID.Text;
            int newsID = Convert.ToInt32(text);
            string content = txtContentNewsDetails.Text;
            DatabaseInteraction.UpdateNewsContent(newsID, content);
            New_Report news = DatabaseInteraction.GetNewsByID(newsID);
            lblContentNewsDetails.Text = news.NewsContent;
            txtContentNewsDetails.Text = "";
            txtContentNewsDetails.Visible = false;
            lblContentNewsDetails.Visible = true;
        }
    }
    protected void btnPostComment_Click(object sender, EventArgs e)
    {
        string text = lblNewsID.Text;
        int newsID = Convert.ToInt32(text);
        Comment c = new Comment()
        {
            NewsID = newsID,
            CommentContent = txtCommentsContent.Text,
            Author =txtUserCommentName.Text,
            TimePost = DateTime.Now
        };
        DatabaseInteraction.AddComment(c);
        ShowComments(newsID);
    }
    protected void lbtnTitleNews2_Click(object sender, EventArgs e)
    {
        mvwUserControl.SetActiveView(vwComments);
        string id = lblNews2ID.Text;
        ShowNewContentDetails(id);
    }
    protected void lbtnTitleNews3_Click(object sender, EventArgs e)
    {
        mvwUserControl.SetActiveView(vwComments);
        string id = lblNews3ID.Text;
        ShowNewContentDetails(id);
    }
    protected void lbtnTitleNews4_Click(object sender, EventArgs e)
    {
        mvwUserControl.SetActiveView(vwComments);
        string id = lblNews4ID.Text;
        ShowNewContentDetails(id);
    }

    public event lbtnReadAll_Click lbtnReadAllClk;
    protected virtual void LbtnReadAllClick(EventArgs e)
    {
        lbtnReadAllClk(this, e);
    }
    protected void lbtnReadAll_Click(object sender, EventArgs e)
    {
       // LbtnReadAllClick(e);
        Response.Redirect("#");
    }
    
}